/*
 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef __APP_TOTA_DATA_HANDLER_H__
#define __APP_TOTA_DATA_HANDLER_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "app_tota_cmd_code.h"

typedef struct
{
	uint8_t		isHasCrcCheck 	:	1;
	uint8_t		reserved		:	7;
	uint8_t		reservedBytes[7];	
} APP_TOTA_START_DATA_XFER_T;

typedef struct
{
	uint8_t		isHasWholeCrcCheck 	:	1;
	uint8_t		reserved		:	7;
	uint8_t		reservedBytes1[3];	
	uint32_t	wholeDataLenToCheck;
	uint32_t	crc32OfWholeData;
} APP_TOTA_STOP_DATA_XFER_T;

typedef struct
{
	uint32_t	segmentDataLen;
	uint32_t	crc32OfSegment;
	uint8_t		reserved[4];
} APP_TOTA_VERIFY_DATA_SEGMENT_T;

typedef struct
{
	uint32_t	reserved;
} APP_TOTA_START_DATA_XFER_RSP_T;

typedef struct
{
	uint32_t	reserved;
} APP_TOTA_STOP_DATA_XFER_RSP_T;

typedef struct
{
	uint32_t	reserved;
} APP_TOTA_VERIFY_DATA_SEGMENT_RSP_T;

typedef struct
{
	uint32_t    dataLenReceivedByPeerDev;
} APP_TOTA_DATA_ACK_T;

typedef void(* receive_data_callback)(uint8_t* ptrData, uint32_t dataLength);

void app_tota_data_reset_env(void);

void app_tota_data_xfer_started(APP_TOTA_CMD_RET_STATUS_E retStatus);
void app_tota_data_xfer_stopped(APP_TOTA_CMD_RET_STATUS_E retStatus);
void app_tota_data_segment_verified(APP_TOTA_CMD_RET_STATUS_E retStatus);
void app_tota_data_received_callback(uint8_t* ptrData, uint32_t dataLength);
void app_tota_send_data(APP_TOTA_TRANSMISSION_PATH_E path, uint8_t* ptrData, uint32_t dataLength);
void app_tota_start_data_xfer(APP_TOTA_TRANSMISSION_PATH_E path, APP_TOTA_START_DATA_XFER_T* req);
void app_tota_stop_data_xfer(APP_TOTA_TRANSMISSION_PATH_E path, APP_TOTA_STOP_DATA_XFER_T* req);
APP_TOTA_CMD_RET_STATUS_E app_tota_data_received(uint8_t* ptrData, uint32_t dataLength);
bool app_tota_data_is_data_transmission_started(void);
void app_tota_handle_received_data(uint8_t* buffer, uint16_t maxBytes);
void app_tota_kickoff_dataxfer(void);
void app_tota_stop_dataxfer(void);
void app_tota_data_ack_received(uint32_t dataLength);
void app_tota_data_received_callback_handler_register(receive_data_callback cb );

#if defined(APP_ANC_TEST)
void app_anc_tota_send_data(APP_TOTA_TRANSMISSION_PATH_E path, uint8_t* ptrData, uint32_t dataLength);
#endif

#ifdef __cplusplus
	}
#endif

#endif // #ifndef __APP_TOTA_DATA_HANDLER_H__

